<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 0px;
            top: 0px;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        // 向右移动之后在向下移动 

        // 封装的形式：
        let boxEle = document.querySelector(".box");
        function move(boxEle, target,dir,cb) {
            var timer = setInterval(() => {
                let num = parseInt(getComputedStyle(boxEle, null)[dir]);
                // currentStyle  :IE 浏览器
                // console.log(num);
                let speed = 10 * (target>num?1:-1);
                if (num === target) {
                    clearInterval(timer);
                    cb();
                } else {
                    boxEle.style[dir] = num + speed + "px";
                }
            }, 10)
        }

        // 画圈  
        // 回调地狱 ；
        // 如何解决回调地狱 ；
        // 1.观察者模式 ；2.promise
        setInterval(()=>{
            move(boxEle,300,"left",function(){
                move(boxEle,300,"top",function(){
                    move(boxEle,0,"left",function(){
                        move(boxEle,0,"top",function(){
                        })
                    })
                })
            })
        },2000)


    </script>
</body>

</html>